<?php
class Authentication_controller extends CI_Controller {
function __construct(){
        parent::__construct();
}
function index(){
		$data['loginErr']=false;
	    $this->load->view('login_view',$data);
	}
	function main_view ($page){
		$data['page']=$page;
		$this->load->view('main_menu_view',$data);
	}
	function login(){
		//Check incoming variables
		$this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
        $this->form_validation->set_rules('password','Password','required|trim|max_length[100]|xss_clean');
				
		if ($this->form_validation->run() == false) {
			$data['loginErr']=false;
			$this->session->set_userdata('validation',FALSE);
			$this->load->view('login_view',$data);			
		} else {
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$id=$this->User_Model->check_login($username,$password);
			$data['loginErr']=false;
			if (! $id){
				//login failure	
				$data['loginErr']=true;
				$this->load->view('login_view',$data);
			}else{
				//logged
				$this->session->set_flashdata('login_error',FALSE);
				$this->session->set_userdata('logged_in',TRUE);
				/*$privileges=$this->User_Model->get_privileges($id);
		     	for($i=0; $i< sizeof($privileges); $i++) {
				    $responce->rows[$i]['cell'] = array($tasks[$i]['id'], 
												;
		        }
   				$this->session->set_userdata('privileges',$privileges);*/			
				$this->session->set_userdata('user_id',$id);
				$this->session->set_userdata('username',$username);
				$this->session->set_userdata('validation',TRUE);
				$data['page']=1;
				$this->load->view('main_menu_view',$data);
			}		
		}
	}
 	function logout(){  	
    	$this->session->sess_destroy();
    	$data['loginErr']=false;
	    $this->load->view('login_view',$data);
    }
    
	function set_current_project(){
		$current_project = $this->input->post('current_project');
		$this->session->set_userdata('current_project', $current_project);
	}
	
}